package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Modelo.musica;

public class daoMusica extends dao {

	public void inserirMusica(musica musica) {

		String sql = "insert into musica (nome,tempoFaixa) values(?,?)";

		try {
			stmt = con.prepareStatement(sql);

			stmt.setString(1, musica.getNome());
			stmt.setDouble(2, musica.getTempoFaixa());

			stmt.execute();
			stmt.close();

		}

		catch (SQLException e) {
			throw new RuntimeException(e);

		}

	}

	public void atualizarMusica(musica musica) {

		String sql = "update musica set nome=? ,tempoFaixa=? where nome =?";

		try {
			stmt = con.prepareStatement(sql);

			stmt.setString(1, musica.getNome());
			stmt.setDouble(2, musica.getTempoFaixa());

			stmt.execute();
			stmt.close();

		}

		catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<musica> listarMusica() {

		String sql = "select * from musica";

		try {
			stmt = con.prepareStatement(sql);

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				musica mu = new musica();

				mu.setNome(rs.getString("nome"));
				mu.setTempoFaixa(rs.getDouble("TempoFaixa"));
				musica.add(mu);
			}
			return listarMusica();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<musica> buscarmMusica(String campo, musica mu) {
		try {
			ArrayList<musica> mu1 = new ArrayList<>();
			String sql1 = "select * from musica where" + campo + "=?";

			stmt = con.prepareStatement(sql1);

			if (campo.equals("nome"))
				stmt.setString(1, mu.getNome());
			else if (campo.equals(" TempoFaixa"))
				stmt.setDouble(1, mu.getTempoFaixa());

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				musica mu2 = new musica();

				mu2.setNome(rs.getString("nome"));
				mu2.setTempoFaixa(rs.getDouble("TempoFaixa"));
				musica.add(mu);

			}

			return mu1;

		} catch (SQLException e) {
			
			throw new RuntimeException(e);
		}
	}

	public void apagarMusica(musica musica) {
		try {

			String sql1 = "delete from musica where nome =?";
			stmt = con.prepareStatement(sql1);
			stmt.setString(1, musica.getNome());

			stmt.executeUpdate(sql1);
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}
